<?php
/**
 * 用户管理控制器
 * User: Administrator
 * Date: 2017/11/20
 * Time: 15:06
 */
namespace app\admin\controller;
use think\Controller;
use think\Db;

class User extends Common {

    public function _initialize(){
        //定义系统的编码
        header("Content-Type:text/html; charset=utf-8");
        header('Content-Type:app/json; charset=utf-8');
        //登录ID
        $this->user_id = session('uid');
    }

    //管理员列表
    public function admin_list(){
        //管理员列表
        $user_list = Db::name('admin')->order('id ASC')->select();
        $count = Db::name('admin')->count();
        $this->assign('total',$count);
        $this->assign('user_list',$user_list);
       // print_r($user_list);die;
        //加载模板
        return $this->fetch();
    }

    /**
     * 添加管理员
     * @return mixed
     */
    public function admin_add(){

        if($data= input()){

            //接收表单数据
            $user_info = array(
              'username'    => trim($data['adminName']),
              'pwd'         => md5($data['password']),
              'addtime'     => time(),
              'ip'          => $_SERVER['REMOTE_ADDR'],
              'status'      =>1,
              'mobile_phone'=> $data['phone'],
              'email'       => $data['email'],
              'role_id'     => $data['adminRole'],
              'remarks'     => $data['remarks']
            );
            //表单验证
//            $validate = Loader::validate('User');
//            if($validate->check($user_info)){
//
//            }
            $user_id = DB::name('admin')->insertGetId($user_info);
        }else{

            return $this->fetch();
        }

    }

    /**
     * 删除管理员
     * id  操作数据id
     */
    public function admindel(){

        if($uid = input('id')){
           $result = Db::name('admin')->where(array('id'=>$uid))->delete();
            if($result){
                echo 1;exit;
            }else{
                echo 0;exit;
            }
        }

    }
    /**
     * 编辑管理员
     * id  操作数据id
     */
    public function admin_edit(){

        if($data = input()){


        }else{
            $id = input('id');
            $result = Db::name('admin')->where(array('id'=>$id))->find();
            $this->assign('userInfo',$result);
            return $this->fetch();
        }



    }

    /**
     * 禁用与启用管理员
     * id  操作数据id
     */
    public function disabled(){

        $data = input();
        if($data['status']){
            Db::name('admin')->where(array('id'=>$data['id']))->update(['status'=>0]);
        }else{
            Db::name('admin')->where(array('id'=>$data['id']))->update(['status'=>1]);
        }
    }


    /**
     * 角色列表
     */
    public function admin_role(){

        $role_list = Db::name('role')->order('id asc')->select();
        //print_r($role_list);
        $this->assign('role_list',$role_list);
        //加载模板
        return $this->fetch();
    }

    /**
     * 添加角色
     */
    public function admin_role_add(){



        //加载模板
        return $this->fetch();
    }



    /**
     * 权限列表
     */
    public function admin_permission(){


        return $this->fetch();
    }

}